package swta.snippet

import net.liftweb.http.S
import net.liftweb.util.Helpers._
import scala.xml.NodeSeq
import scala.xml.Node
import scala.xml.Group
import scala.xml.Text
import net.liftweb.util.Full

import swta.model.Item

class ItemManager {
  def addItem = (new Item).toForm(
    Full("Post"),
    (item: Item) => {
      item.save
      S.redirectTo("view_items")
    })
  
  def viewItems(xhtml: Group): NodeSeq = {
    val items = Item.findAll
    
    items match {
      case Nil => <div>No items existing.</div>
      
      case items => items.flatMap(
        item => bind("item", xhtml,
          "name" -> item.name,
          "description" -> item.description.toString,
          "price" -> item.price.toString))
    }
  }
}
